﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Client.Demo
{
    public partial class MainPage : UserControl,jacSocketClient.IProcessor 
    {
        jacSocketClient.JFormat jfm = new jacSocketClient.JFormat();
        jacSocketClient.ClientControler cc;

        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            this.bt_send.Click += new RoutedEventHandler(bt_send_Click);
        }

        void bt_send_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(tb_text.Text))
            {
                msgclass mc = new msgclass();
                mc.userId = "test";
                mc.msg=tb_text.Text;
                cc.SendData(jfm.ToJson<msgclass>(mc));
            }
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            cc = new jacSocketClient.ClientControler("127.0.0.1", 4502, this);
        }

        #region IProcessor Members

        public void ProcessData(object receivedData)
        {
            var data = jfm.Deserialize<msgclass>(receivedData.ToString());
            // show Msg
            lb.Items.Insert(0, data.userId + ": " + data.msg + ">post time:" + data.sendTime.ToString());  
        }

        public void connetData(object conData)
        {
            tbk_state.Text = conData.ToString();
        }

        public void OnError(object errData)
        {
            tbk_state.Text = errData.ToString();
        }

        #endregion
    }
}
